package com.hmyd.hmyd_android.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.CouponListAdapter;
import com.hmyd.hmyd_android.adapter.CouponListPagerAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.dto.AddInterestsCouponCount;
import com.hmyd.hmyd_android.dto.CouponCount;
import com.hmyd.hmyd_android.fragment.AddInterestsTicketFragment;
import com.hmyd.hmyd_android.fragment.CouponListFragment;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.ViewPagerIndicator;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 现金券列表
 */
@ContentView(R.layout.activity_coupon_list)
public class CouponListActivity extends BasicActivity {
	private final int COUPON_INTRO = 1;// 现金券使用情况
	private final int ADDINTERESTS = 2;// 加息券使用情况
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	private List<Fragment> mTabContents = new ArrayList<Fragment>();
	private CouponListPagerAdapter mAdapter;
	private List<String> mDatas = Arrays.asList("可使用", "已使用", "已过期");
	private List<String> mType = Arrays.asList("usable", "used", "expired");

	@ViewInject(R.id.id_indicator)
	private ViewPagerIndicator mIndicator;
	@ViewInject(R.id.id_vp)
	private ViewPager mViewPager;

	@OnClick({ R.id.case_ticket, R.id.add_interests_ticket })
	public void Click(View view) {
		switch (view.getId()) {
		case R.id.case_ticket:
			setBackground(false);
			onRequest(COUPON_INTRO, UrlConstant.COUPONCPUNT_WITH_PICK, HttpMethod.GET, null);
			break;
		case R.id.add_interests_ticket:
			setBackground(true);
			onRequest(ADDINTERESTS, UrlConstant.COUPONEXTRA, HttpMethod.GET, null);
			break;

		default:
			break;
		}
	}

	private void setBackground(boolean isPress){
		if (isPress) {
			findViewById(R.id.case_ticket).setBackground(getResources().getDrawable(R.drawable.case_ticket2));
			findViewById(R.id.add_interests_ticket).setBackground(getResources().getDrawable(R.drawable.add_interests_ticket2));
			((TextView)findViewById(R.id.case_ticket)).setTextColor(getResources().getColor(R.color.text_orange));
			((TextView)findViewById(R.id.add_interests_ticket)).setTextColor(getResources().getColor(R.color.white));
		} else {
			findViewById(R.id.case_ticket).setBackground(getResources().getDrawable(R.drawable.case_ticket));
			findViewById(R.id.add_interests_ticket).setBackground(getResources().getDrawable(R.drawable.add_interests_ticket));
			((TextView)findViewById(R.id.case_ticket)).setTextColor(getResources().getColor(R.color.white));
			((TextView)findViewById(R.id.add_interests_ticket)).setTextColor(getResources().getColor(R.color.text_orange));
		}
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		Type type = new TypeToken<CouponCount>() {
		}.getType();
		CouponCount couponCount = gson.fromJson(msg, type);

		if (couponCount != null) {
			mDatas = Arrays.asList("可使用("+couponCount.usable+")", "已使用("+couponCount.used+")", "已过期("+couponCount.expired+")");
		} else {
			mDatas = Arrays.asList("可使用(0)", "已使用(0)", "已过期(0)");
		}

		switch (postId) {
		case COUPON_INTRO:
			initDatas();
			break;
		case ADDINTERESTS:
			initExcDatas();
		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
	}

	// 初始化现金券
	private void initDatas(){
		if (mAdapter != null) {
			mAdapter.destroySelf();
		}
		mTabContents.clear();
		
		for (int i = 0;i<mType.size();i++) {
			CouponListFragment fragment = new CouponListFragment();
			Bundle bundle = new Bundle();
			bundle.putString("type", mType.get(i));
			fragment.setArguments(bundle);
			mTabContents.add(fragment);
		}

		mAdapter = new CouponListPagerAdapter(getSupportFragmentManager(), mTabContents);
		mIndicator.setTabItemTitles(mDatas);
		mViewPager.setAdapter(mAdapter);
		mIndicator.setViewPager(mViewPager, 0);
	}
	
	// 初始化加息券
	private void initExcDatas(){
		if (mAdapter != null) {
			mAdapter.destroySelf();
		}
		mTabContents.clear();
		for (int i = 0;i<mType.size();i++) {
			AddInterestsTicketFragment fragment = new AddInterestsTicketFragment();
			Bundle bundle = new Bundle();
			bundle.putString("type", mType.get(i));
			fragment.setArguments(bundle);
			mTabContents.add(fragment);
		}

		mAdapter = new CouponListPagerAdapter(getSupportFragmentManager(), mTabContents);

		mIndicator.setTabItemTitles(mDatas);
		mViewPager.setAdapter(mAdapter);
		mIndicator.setViewPager(mViewPager, 0);
	}

	@Override
	protected void setParams() {
//		home_title_txt.setText(getString(R.string.coupons));
		home_right.setVisibility(View.VISIBLE);
		home_right.setText(getString(R.string.assets_instructions));
		home_title.setOnTouchListener(touch);
		CouponCount couponCount = (CouponCount) this.getIntent().getSerializableExtra("couponCount");
		if(couponCount !=null) {
			mDatas = Arrays.asList("可使用("+couponCount.usable+")", "已使用("+couponCount.used+")", "已过期("+couponCount.expired+")");
			initDatas();
		}
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		if(AppBasic.app!=null && !TextUtils.isEmpty(AppBasic.app.couponUseInstructions)){
			Utils.jumpURL(this, AppBasic.app.couponUseInstructions, "现金券使用说明","",false);
		}
	}
}
